import asyncio


from pyppeteer import launch


async def main():
    browser = await launch()
    page = await browser.newPage()
    # 设置页面视图大小
    await page.setViewport(viewport={'width': 1280, 'height': 800})
    # 是否启用JS，enabled设为False，则无渲染效果
    await page.setJavaScriptEnabled(enabled=True)

    # 反爬js
    js_text = """
    () =>{ 
        Object.defineProperties(navigator,{ webdriver:{ get: () => false } });
        window.navigator.chrome = { runtime: {},  };
        Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
        Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], });
     }
    """
    await page.evaluateOnNewDocument(js_text)  # 本页刷新后值不变，自动执行js

    await page.goto('https://www.runoob.com/')

    # 获取所有 html 内容
    # print(await page.content())

    """
    # Pyppeteer 三种解析方式
    Page.querySelector()  # 选择器
    Page.querySelectorAll()
    Page.xpath()  # xpath  表达式
    # 简写方式为：
    Page.J(), Page.JJ(), and Page.Jx()
    """
    title_list = await page.xpath('/html/body/div[4]/div/div[2]/div')
    # print(title_list)
    for title in title_list:
        a = await title.xpath('./a')
        h2 = await title.xpath('./h2')
        link = await (await a[0].getProperty("href")).jsonValue()
        name = await (await h2[0].getProperty("textContent")).jsonValue()
        print(name, link)

    await page.close()
    await browser.close()
    # sleep(10)

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())